package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeCreditExchangeActivity extends Activity {

    private EditText mVCode = null;
    private TextView mChangeVCode = null;
    private ImageView mVCodeImg = null;
    private NetworkImpl mCreditExchangeNetwork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_credit_exchange);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_exchange);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomeCreditExchangeActivity.this.finish();
            }

        });

        String description = String.format(
                  getResources().getString(R.string.home_user_credit_exchange_description)
                , getIntent().getExtras().getString("title")
                , getIntent().getExtras().getString("cost"));

        TextView des = (TextView)findViewById(R.id.home_user_credit_exchange_description);
        des.setText(description);

        mCreditExchangeNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        mVCode = (EditText)findViewById(R.id.home_user_credit_exchange_cerCode);
        mChangeVCode = (TextView)findViewById(R.id.home_user_credit_exchange_cerCode_change);
        mChangeVCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mChangeVCode.getPaint().setAntiAlias(true);
        mChangeVCode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mCreditExchangeNetwork.getVerifyCodeAsync();
            }

        });
        mVCodeImg = (ImageView)findViewById(R.id.home_user_change_psw_cerCode_img);

        Button okbt = (Button)findViewById(R.id.home_user_credit_exchange_confirmed);
        okbt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (mVCode.getText().length() <= 0) {
                    showComplete(false, getResources().getString(R.string.home_user_settings_change_psw_vcode_failed));
                    return;
                }
                mCreditExchangeNetwork.creditExchangeAsync(123, 1, mVCode.getText().toString());
            }

        });

        Button cancelbt = (Button)findViewById(R.id.home_user_credit_exchange_canceld);
        cancelbt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomeCreditExchangeActivity.this.finish();
            }

        });
    }

    private void showComplete(final boolean success, String msg) {
        AlertDialog.Builder build = new AlertDialog.Builder(this);
        String myMsg = success?getResources().getString(R.string.home_user_credit_exchange_success):msg;
        build.setMessage(myMsg);
        build.setPositiveButton(getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (success)
                    HomeCreditExchangeActivity.this.finish();
            }
        });

        build.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_credit_exchange, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeCreditExchangeActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_CREDIT_EXCHANGE:
                Intent tmp = new Intent(HomeCreditExchangeActivity.this, ExchangeResultActivity.class);
                tmp.putExtra("result", getResources().getString(R.string.home_user_credit_exchange_success));
                startActivity(tmp);
                mVCode.setText("");
                break;
            case NetworkImpl.NET_WORK_ID_GET_VERIFY_CODE:
                break;
            }
        }
    }

}
